package net.towee.model.account.impl;

import java.util.Date;

import net.towee.model.account.AccountGroup;
import net.towee.model.account.Role;
import net.towee.model.group.GroupIdentity;

import com.google.code.morphia.annotations.Embedded;
import com.google.code.morphia.annotations.Indexed;
import com.google.code.morphia.annotations.Reference;

/**
 * Implementation of information about group that stored in user data
 * @author c58
 */
@Embedded 
public class AccountGroupImpl implements AccountGroup {	
	private Date enterDate;
	@Reference private Role role;
	@Indexed @Reference private GroupIdentity identity;
	
	public AccountGroupImpl(){}
	public AccountGroupImpl(GroupIdentity identity, Role role) {
		this.enterDate = new Date();
		this.role = role;
		this.identity = identity;
	}
	
	@Override
	public Role getRole() {
		return role;
	}

	@Override
	public GroupIdentity getGroupIdentity() {
		return identity;
	}

	@Override
	public Date getEnterDate() {
		return enterDate;
	}

	@Override
	public boolean equals(Object o){
		if(o == this)
			return true;
		else if(o instanceof AccountGroup && ((AccountGroup)o).getGroupIdentity().getId().equals( getGroupIdentity().getId() ))
			return true;
		else if(o instanceof GroupIdentity && this.getGroupIdentity().getId().equals( ((GroupIdentity)o).getId() ))
			return true;
		
		return false;
	}
	
	@Override
	public void setRole(Role role) {
		this.role = role;
	}
}
